package guyubao.com.listener;

import guyubao.com.entity.EmailModel;
import guyubao.com.utils.SendMailMessageUtils;
import guyubao.com.utils.SendMailType;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/***
 * 监听发送邮箱
 */
@Component  //注册为Bean
@RabbitListener(queues = "mail")
@Slf4j
public class MailQueueListener {
    @Resource
    SendMailMessageUtils sendMailMessageUtils;
    @Resource
    SendMailType sendMailType;
    @RabbitHandler
    public void sendMailMessage(Map<String,Object> data){
        String type = (String) data.get("type");
        String email = (String) data.get("email");
        Integer code = (Integer) data.get("code");
        if (type==null||email==null||code==null){
            log.warn("rabbitmq接收的邮件参数有空位:type:%s,email:%s,code:%s",type,email,code);
            return;
        }
        EmailModel emailModel = new EmailModel();
        emailModel.setReceiverEmailAddress(email);
        emailModel.setEmailContent(code.toString());
        emailModel.setEmailTheme(sendMailType.parseEmailSubject(type));
        sendMailMessageUtils.sendEmailAsSysExceptionHtml(emailModel);
    }
}
